package com.atcapp.server.controller;

import com.atcapp.server.dao.*;
import com.atcapp.server.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ActionController {
    @Autowired
    private StuActRepository stuActRepository;
    @Autowired
    private TeaActRepository teaActRepository;
    @Autowired
    private UserStuRepository userStuRepository;
    @Autowired
    private UserTeaRepository userTeaRepository;
    @Autowired
    private StunoteRepository stunoteRepository;
    @Autowired
    private TeanoteRepository teanoteRepository;
    /*
    * 学生活动展示
    * */
    @PostMapping(value = "/StuActShow")
    public List<StuactDAO>  StuActShow(@RequestParam("username")String userID,
                                             @RequestParam("password")String password,
                                             @RequestParam("permission")Integer permission) {
        if(permission==1){
            StudentDAO user = userStuRepository.findBystudentID(userID);
            if (user != null){

                if(user.getPassword().equals(password)) {
                    return stuActRepository.findAll();
                }
            }}

        return null;
    }
     /*
    *  教师活动展示
    * */
     @PostMapping(value = "/TeaActShow")
     public List<TeaactDAO>  TeaActShow(@RequestParam("username")String userID,
                                              @RequestParam("password")String password,
                                              @RequestParam("permission")Integer permission) {
         if(permission==2){
             TeacherDAO user = userTeaRepository.findByteacherID(userID);
             if (user != null){

                 if(user.getPassword().equals(password)) {
                     return teaActRepository.findAll();
                 }
             }}

         return null;
     }
      /*
    *  教师活动发布
    * */
      @PostMapping(value = "/TeaActUp")
      public TeaactDAO TeaActShow(TeaactDAO teaactDAO) {
          if(userTeaRepository.findByteacherID(teaactDAO.getInitiator())!=null){
              teaactDAO.setActname(teaactDAO.getActname());
              teaactDAO.setId(teaactDAO.getId());
              teaactDAO.setInitiator(teaactDAO.getInitiator());
              teaactDAO.setPlace(teaactDAO.getPlace());
              teaactDAO.setRemainnum(teaactDAO.getRemainnum());
              teaactDAO.setTime(teaactDAO.getTime());
              teaactDAO.setIntroduction(teaactDAO.getIntroduction());
              return teaActRepository.save(teaactDAO);
          }

          return null;
      }
       /*
    *  学生活动会议记录
    * */
       @PostMapping(value = "/StuNote")
       public StunoteDAO TeaActShow(StunoteDAO stunoteDAO) {
           if(userStuRepository.findBystudentID(stunoteDAO.getStuid().toString())!=null){
               stunoteDAO.setId(stunoteDAO.getId());
               stunoteDAO.setActevaluate(stunoteDAO.getActevaluate());
               stunoteDAO.setActid(stunoteDAO.getActid());
               stunoteDAO.setActname(stunoteDAO.getActname());
               stunoteDAO.setActreview(stunoteDAO.getActreview());
               stunoteDAO.setStuid(stunoteDAO.getStuid());
               return stunoteRepository.save(stunoteDAO);
           }

           return null;
       }

       /*
    *  教师活动会议记录
    * */
       @PostMapping(value = "/TeaNote")
       public TeanoteDAO TeaActShow(TeanoteDAO teanoteDAO) {
           if(userStuRepository.findBystudentID(teanoteDAO.getTeaid().toString())!=null){
               teanoteDAO.setId(teanoteDAO.getId());
               teanoteDAO.setActevaluate(teanoteDAO.getActevaluate());
               teanoteDAO.setActid(teanoteDAO.getActid());
               teanoteDAO.setActname(teanoteDAO.getActname());
               teanoteDAO.setActreview(teanoteDAO.getActreview());
               teanoteDAO.setTeaid(teanoteDAO.getTeaid());
               return teanoteRepository.save(teanoteDAO);
           }

           return null;
       }
}
